﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MCromwell.StaffIntranet.Core
{
    public class UniqueStaffNameSpecification : ISpecification<Staff>
    {
        protected IStaffRepository staffRepository;

        public UniqueStaffNameSpecification(IStaffRepository staffRepository)
        {
            if (staffRepository == null)
                throw new ArgumentNullException("staffRepository", "staffRepository cannot be null");

            this.staffRepository = staffRepository;
        }

        public DomainObjectValidationResult ResultsOf(Staff item)
        {
            var result = new DomainObjectValidationResult();
            Staff staffWithSameName = staffRepository.FindWithSame(item.LastName, item.FirstName);
            if ((staffWithSameName != null) && (staffWithSameName.Id != item.Id))
                result.Register(new ValidationResultItem("staffwithsamenameexists", string.Format("Staff with firstname: '{0}', lastname: '{1}' already exists", staffWithSameName.FirstName, staffWithSameName.LastName)));

            return result;
        }
    }
}
